export const findComponent = (context, componentName) => {
  if (!context || !componentName) return

  let parent = context.$parent
  while (parent) {
    if (parent.componentName !== componentName) {
      parent = parent.$parent
    } else {
      return parent
    }
  }
  return false
}
export const findComponentDirect = (context, componentName) => {
  if (!context || !componentName) return

  const parent = context.$parent
  if (parent.componentName === componentName) {
    return parent
  }
  return false
}
export const findComponents = (context, componentName) => {
  if (!context || !componentName) return

  const parents = []
  const parent = context.$parent
  if (parent) {
    if (parent.componentName === componentName) parents.push(parent)
    return parents.concat(findComponents(parent, componentName))
  }
  return parents
}
export const findComponentChildrens = (context, componentName) => {
  if (!context || !componentName) return

  return context.$children.reduce((components, child) => {
    if (child.componentName === componentName) components.push(child)
    const foundChilds = findComponentChildrens(child, componentName)
    return components.concat(foundChilds)
  }, [])
}
export const findComponentDirectChildrens = (context, componentName) => {
  if (!context || !componentName) return
  return context.$children.filter((item) => {
    return item.componentName === componentName
  })
}
export const findBrothersComponents = (context, componentName) => {
  if (!context || !componentName) return
  const component = context.$parent.$children.filter((item) => {
    return item.componentName === componentName
  })
  // 当前元素的index
  const currentIndex = component.findIndex((item) => {
    return item._uid === context._uid
  })
  // 去除当前元素
  component.splice(currentIndex, 1)
  return component
}
